package com.molichuxing.gateway.bff.stock.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.stock.convertor.Convert;
import com.molichuxing.gateway.bff.stock.entity.response.StockRecordBaseVo;
import com.molichuxing.gateway.bff.stock.entity.response.StockRecordVo;
import com.molichuxing.gateway.bff.stock.services.StockRecordBffService;
import com.molichuxing.services.business.dto.response.DealerStoreInfoBizDto;
import com.molichuxing.services.business.dto.response.StockRecordBizDto;
import com.molichuxing.services.business.dto.response.VehicleNameBizDto;
import com.molichuxing.services.business.service.StockRecordBizService;
import com.molichuxing.services.business.service.StoreBizService;
import com.molichuxing.services.business.service.VehicleBizService;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.dto.response.OwnershipDto;
import com.molichuxing.services.infrastructure.service.CarService;
import com.molichuxing.services.infrastructure.service.OwnershipService;
import com.molichuxing.services.property.CarsOperateStatusEnum;
import com.molichuxing.services.property.StockCarStockTypeEnum;

/**
 * 
 * @author liyg
 * 库存历史bff服务实现类
 * 2019-08-26
 *
 */
@Service("stockRecordBffService")
public class StockRecordBffServiceImpl implements StockRecordBffService{

	@Resource
	private StockRecordBizService stockRecordBizService;
	
	@Resource
	private CarService carService;
	
	@Resource
	private VehicleBizService vehicleBizService;
	
	@Resource
	private StoreBizService storeBizService;
	
	@Resource
	private OwnershipService ownershipService;

	/**
	 *获取库存记录列表
	 * @param map 参数
	 *key:vin        String    车架号 非必填
	 *key:licencePlate String    车牌号 非必填
	 *key:vehicleId  Integer 车型id 非必填
	 *key:type       Integer 出入库类型 非必填
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 * @throws Exception 
	 */
	@Override
	public Paged<StockRecordVo> getStockRecordList(Map<String, Object> map)throws Exception { 
		
		
		String vin = ObjectCastUtil.castString(map.get("vin"));
		String licencePlate = ObjectCastUtil.castString(map.get("licencePlate"));
		if(StringUtils.isNoneBlank(vin) || StringUtils.isNoneBlank(licencePlate)) {
			CarDto carDto = carService.getByVinLicencePlate(vin, licencePlate);
			if(null == carDto) {
				return new Paged<StockRecordVo>();
			}
			map.put("carId", carDto.getId());
		}
		
		Paged<StockRecordBizDto> stockRecordPaged = stockRecordBizService.getStockRecordList(map);
		if(null == stockRecordPaged) {
			return new Paged<StockRecordVo>();
		}else if(stockRecordPaged.isEmpty()) {
			return new Paged<StockRecordVo>(null,stockRecordPaged.getTotal(),stockRecordPaged.getPageSize());
		}
		
		List<StockRecordBizDto> stockRecordBizDtoList = stockRecordPaged.getList();
		
		//获取车辆id集合
		List<Integer> carIds=new ArrayList<>();
		//获取车型id集合
		List<Integer> vehicleIds=new ArrayList<>();
		//获取发车方门店id集合
		List<Integer> storeIdOuts=new ArrayList<>();
		//获取收车方门店id集合
		List<Integer> storeIdIns=new ArrayList<>();
		//获取产权方id集合
		List<Integer> ownershipIds=new ArrayList<>();
		
		for (StockRecordBizDto dto : stockRecordBizDtoList) {
			carIds.add(dto.getCarId());
			vehicleIds.add(dto.getVehicleId());
			
			Integer outStockType = dto.getOutStockType();
			if(null != outStockType) {
				if(StockCarStockTypeEnum.STORE.getValue() == outStockType) {
					storeIdOuts.add(dto.getOutStoreId());
				}
			}
			
			Integer inStockType = dto.getInStockType();
			if(null != inStockType) {
				if(StockCarStockTypeEnum.STORE.getValue() == inStockType) {
					storeIdIns.add(dto.getInStoreId());
				}
			}
		}
		
		//获取车辆信息
		Map<Integer, CarDto> carMap = carService.getCarByIds(carIds);
		if(!carMap.isEmpty()) {
			for (Integer key : carMap.keySet()) {
				CarDto carDto = carMap.get(key);
				if(null != carMap) {
					ownershipIds.add(carDto.getOwnershipId());
				}
			}
		}
			
		
		//获取车型信息
		Map<Integer, VehicleNameBizDto> vehicleNameMap= vehicleBizService.getCarByModelNameList(vehicleIds);
		
		//获取发车方门店信息
		Map<Integer, DealerStoreInfoBizDto> dealerOutMap =null;
		if(!storeIdOuts.isEmpty()) {
			dealerOutMap= storeBizService.getDealerStoreInfoMap(storeIdOuts);
		}
		
		//获取发车方门店信息
		Map<Integer, DealerStoreInfoBizDto> dealerInMap =null;
		if(!storeIdIns.isEmpty()) {
			dealerInMap=storeBizService.getDealerStoreInfoMap(storeIdIns);
		}
				
		//获取产权方信息
		Map<Integer, OwnershipDto> ownershipMap=null;
		if(!ownershipIds.isEmpty()) {
			ownershipMap= ownershipService.getOwnershipListByIds(ownershipIds);
		}
		
		List<StockRecordVo> stockRecordVoList = getRecordVoList(vehicleNameMap,carMap,dealerOutMap,dealerInMap,ownershipMap,stockRecordBizDtoList);
		
		Paged<StockRecordVo> paged = new Paged<>(stockRecordVoList,stockRecordPaged.getTotal(),stockRecordPaged.getPageSize());
		return paged;
	}
	
	/**
	 * 获取库存记录返回数据
	 * @param vehicleNameMap
	 * @param carMap
	 * @param dealerOutMap
	 * @param dealerInMap
	 * @param ownershipMap
	 * @param stockRecordBizDtoList
	 * @return
	 */
	private  List<StockRecordVo>  getRecordVoList(Map<Integer, VehicleNameBizDto> vehicleNameMap,Map<Integer, CarDto> carMap,
			Map<Integer, DealerStoreInfoBizDto> dealerOutMap,Map<Integer, DealerStoreInfoBizDto> dealerInMap,
			Map<Integer, OwnershipDto> ownershipMap,List<StockRecordBizDto> stockRecordBizDtoList){
       
		List<StockRecordVo> stockRecordVoList=new ArrayList<>();
		
		for (StockRecordBizDto dto : stockRecordBizDtoList) {
			StockRecordVo vo = new StockRecordVo();
			BeanUtils.copyProperties(dto, vo);
			
			//获取车辆信息
			CarDto carDto= null == carMap ? null : carMap.get(dto.getCarId());
			vo.setVin(carDto == null ? null : carDto.getVin());
			vo.setLicencePlate(carDto == null ? null : carDto.getLicencePlate());
			vo.setOwnershipId(carDto == null ? null : carDto.getOwnershipId());
			
			//获取车型信息
			VehicleNameBizDto vehicleNameBizDto= null == vehicleNameMap ? null : vehicleNameMap.get(dto.getVehicleId());
			vo.setVehicleName(vehicleNameBizDto== null ? null : 
				vehicleNameBizDto.getSubBrandName()+vehicleNameBizDto.getSeriesName()+vehicleNameBizDto.getModelName());
			vo.setVehicleImg(vehicleNameBizDto== null ? null : 
				vehicleNameBizDto.getWhiteFigure());
			
			//获取产权方信息
			OwnershipDto ownershipDto= null == ownershipMap ? null : ownershipMap.get(vo.getOwnershipId());
			vo.setOwnershipName(ownershipDto == null ? null : ownershipDto.getName());
			
			//获取发车方门店信息
			Integer outStockType = dto.getOutStockType();
			if(null != outStockType) {
				if(StockCarStockTypeEnum.STORE.getValue() == outStockType) {
					DealerStoreInfoBizDto  storeDtoOut = null == dealerOutMap ? null : dealerOutMap.get(dto.getOutStoreId());
					vo.setOutStockName(storeDtoOut == null ? null : storeDtoOut.getDealerShortName()+"-"+storeDtoOut.getDealerShortName());
				}
			}
			
			//获取收车方门店信息
			Integer inStockType = dto.getInStockType();
			if(null != inStockType) {
				if(StockCarStockTypeEnum.STORE.getValue() == dto.getInStockType()) {
					DealerStoreInfoBizDto  storeDtoIn = null == dealerInMap ? null : dealerInMap.get(dto.getInStoreId());
					vo.setInStockName(storeDtoIn == null ? null : storeDtoIn.getDealerShortName()+"-"+storeDtoIn.getDealerShortName());
				}
			}
			
			stockRecordVoList.add(vo);
		}
		return stockRecordVoList;
	}
	
	/**
	 * 根据车辆id获取库存历史流转记录
	 * @param carId
	 * @return
	 * @throws Exception 
	 */
	@Override
	public List<StockRecordBaseVo> getListByCarId(Integer carId) throws Exception {
		
		List<StockRecordBizDto> list = stockRecordBizService.getListByCarId(carId);
		if(null == list || list.size() <= 0) {
			return null;
		}
		
		//获取发车方门店id集合
		List<Integer> storeIdOuts=new ArrayList<>();
		//获取收车方门店id集合
		List<Integer> storeIdIns=new ArrayList<>();
		
		for (StockRecordBizDto dto : list) {
			
			Integer outStockType = dto.getOutStockType();
			if(null != outStockType) {
				if(StockCarStockTypeEnum.STORE.getValue() == outStockType) {
					storeIdOuts.add(dto.getOutStoreId());
				}
			}
			
			Integer inStockType = dto.getInStockType();
			if(null != inStockType) {
				if(StockCarStockTypeEnum.STORE.getValue() == inStockType) {
					storeIdIns.add(dto.getInStoreId());
				}
			}
		}

		//获取发车方门店信息
		Map<Integer, DealerStoreInfoBizDto> dealerOutMap =null;
		if(!storeIdOuts.isEmpty()) {
			dealerOutMap= storeBizService.getDealerStoreInfoMap(storeIdOuts);
		}
		
		//获取发车方门店信息
		Map<Integer, DealerStoreInfoBizDto> dealerInMap =null;
		if(!storeIdIns.isEmpty()) {
			dealerInMap=storeBizService.getDealerStoreInfoMap(storeIdIns);
		}

		//获取库存记录返回数据
		List<StockRecordBaseVo> recordVoList = getRecordVoList(dealerOutMap,dealerInMap,list);
				
		return recordVoList;
	}
	
	/**
	 *  获取库存记录返回数据
	 * @param dealerOutMap
	 * @param dealerInMap
	 * @param stockRecordBizDtoList
	 * @return
	 */
	private  List<StockRecordBaseVo>  getRecordVoList(Map<Integer, DealerStoreInfoBizDto> dealerOutMap,
			Map<Integer, DealerStoreInfoBizDto> dealerInMap,List<StockRecordBizDto> stockRecordBizDtoList){
       
		List<StockRecordBaseVo> stockRecordVoList=new ArrayList<>();
		
		for (StockRecordBizDto dto : stockRecordBizDtoList) {
			StockRecordBaseVo vo = Convert.toStockRecordBaseVo(dto);
			
			//获取发车方门店信息
			Integer outStockType = dto.getOutStockType();
			if(null != outStockType) {
				if(StockCarStockTypeEnum.STORE.getValue() == outStockType) {
					DealerStoreInfoBizDto  storeDtoOut = null == dealerOutMap ? null : dealerOutMap.get(dto.getOutStoreId());
					vo.setOutStockName(storeDtoOut == null ? null : storeDtoOut.getDealerShortName()+"-"+storeDtoOut.getStoreShortName());
				}
			}
			
			//获取收车方门店信息
			Integer inStockType = dto.getInStockType();
			if(null != inStockType) {
				if(StockCarStockTypeEnum.STORE.getValue() == dto.getInStockType()) {
					DealerStoreInfoBizDto  storeDtoIn = null == dealerInMap ? null : dealerInMap.get(dto.getInStoreId());
					vo.setInStockName(storeDtoIn == null ? null : storeDtoIn.getDealerShortName()+"-"+storeDtoIn.getStoreShortName());
				}
			}
			
			stockRecordVoList.add(vo);
		}
		return stockRecordVoList;
	}
}
